import 'package:flutter/material.dart';
import 'package:ls_work/a_elder/models/evaluation_entity.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/widgets/click_item.dart';
import 'package:ls_work/mvp/base_page_state.dart';
import 'package:ls_work/a_elder/presenter/evaluate_presenter.dart';
import 'package:ls_work/provider/base_list_provider.dart';
import 'package:ls_work/widgets/my_refresh_divider_list.dart';
import 'package:ls_work/widgets/state_layout.dart';
import 'package:provider/provider.dart';

class ElderEvaluateReportPage extends StatefulWidget {
  final int? customerId;
  ElderEvaluateReportPage({Key? key,  this.customerId});

  @override
  ElderEvaluateReportPageState createState() => ElderEvaluateReportPageState();
}

class ElderEvaluateReportPageState
    extends BasePageState<ElderEvaluateReportPage, EvaluatePresenter>
    with AutomaticKeepAliveClientMixin {
  BaseListProvider<EvaluateInfo> provider = BaseListProvider<EvaluateInfo>();

  int _page = 1;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    print(widget.customerId);

    /// 默认为加载中状态
    provider.setStateType(StateType.empty);
    _page = 1;

    print(presenter);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      /// 接口请求
      presenter.fectEvaluateData(widget.customerId, _page, true);
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ChangeNotifierProvider<BaseListProvider<EvaluateInfo>>(
        create: (_) => provider,
        child: Consumer<BaseListProvider<EvaluateInfo>>(
            builder: (_, provider, __) {
          return DeerSeparatedListView(
            key: Key('medical_report_list'),
            itemCount: provider.list.length,
            stateType: provider.stateType,
            onRefresh: _onRefresh,
            loadMore: _loadMore,
            hasMore: provider.hasMore,
            itemBuilder: (_, index) {
              return ClickItem(
                title: provider.list[index].evaluationTypeName??"",
                content: provider.list[index].evaluationTime??"",
                // onTap: () {
                // },
              );
            },
            separatorBuilder: (_, int index) {
              return Container(
                width: double.infinity,
                height: 1,
                color: Colours.bg_color,
              );
            },
          );
        }));
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.fectEvaluateData(widget.customerId, _page, false);
  }

  Future _loadMore() async {
    _page++;
    await presenter.fectEvaluateData(widget.customerId, _page, false);
  }

  @override
  EvaluatePresenter createPresenter() {
    return EvaluatePresenter();
  }
}
